const Koa = require('koa');

const convert = require('koa-convert');
const bodyParser = require('koa-bodyparser');
const koaBody = require('koa-body');
const json = require('koa-json');
const logger = require('koa-logger');
const cors = require('koa2-cors'); // cross domain
const session = require("koa-session2");

const midLogger = require('./middleware/logger');
const midAuth = require('./middleware/auth');
const midRemember = require('./middleware/remember');
const router = require('./routes/allRoute');

const app = new Koa();

app.use(convert.compose(
    session({key: "SESSIONID"}),
    koaBody({multipart: true}),
    bodyParser(),
    json(),
    logger(),
    cors(),
    midRemember(),
    midAuth()
));


//assert resource
app.use(convert(require('koa-static')(__dirname + '/public')));

//middleware
app.use(convert(midLogger()));

app.use(router.routes(), router.allowedMethods());

module.exports = app;
